﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace IRC.IRepairClouds.WebClient
{
    public static class Utils
    {
        private static bool? _isDebug;

        public static bool IsDebug
        {
            get
            {
                if (_isDebug == null)
                {
                    var config = WebConfigurationManager.OpenWebConfiguration("~");
                    System.Web.Configuration.CompilationSection compilation = config.GetSection("system.web/compilation") as System.Web.Configuration.CompilationSection;
                    if (compilation != null)
                        _isDebug = compilation.Debug;
                    else
                        _isDebug = false;
                }
                return _isDebug.Value;
            }
        }

        public static string MakeAbsolutePath(string url)
        {
            if (string.IsNullOrEmpty(url))
                return null;

            string urlLow = url.ToLower();
            if (urlLow.StartsWith("http://") || urlLow.StartsWith("https://"))
                return url;

            int pq = url.IndexOf('?');
            string query = null;
            if (pq != -1)
            {
                query = url.Substring(pq);
                url = url.Remove(pq);
            }
            url = VirtualPathUtility.Combine(VirtualPathUtility.GetDirectory(HttpContext.Current.Request.Url.AbsolutePath), url);
            return VirtualPathUtility.ToAbsolute(url) + query;
        }
    }
}